from django.test import TestCase
from django.core.urlresolvers import reverse
from django.urls import resolve
from django.test import TestCase
from .views import first,board_topics
from .models import Board

# Create your tests here.

"""
# 测试的是请求该URL后返回的响应状态码.

class FirstTests(TestCase):
    def first_test(self):
        url = reverse('first')
        response = self.client.get(url)
        self.assertEquals(response.status_code,200)


    # 使用resolve函数,可以将浏览器发起请求的URL与urls.py模块中列出的URL进行匹配.该测试用于确定URL(/) 返回first视图.
    def TestUrl(self):
        view = resolve('/')
        self.assertEquals(view.func,home)

class BoardTopicsTests(TestCase):
    def setUp(self):
        Board.objects.create(name='Django',description='Django board.')

    def test_board_topics_view_success_status_code(self):
        url = reverse('board_topics',kwargs={'pk': 1})
        response = self.client.get(url)
        # self.assertEquals(response.status_code,200)
        self.assertEquals(response.status_code,404)


    def test_board_topics_view_not_found_status_code(self):
        url = reverse('board_topics',kwargs={'pk':99})
        response = self.client.get(url)
        self.assertEquals(response.status_code,404)

    def test_board_topics_url_resolves_board_topics_view(self):
        view = resolve('/boards/1/')
        self.assertEquals(view.func,board_topics)

"""

"""
在first_test方法中,创建了一个Board实例用来测试.
在first_test方法中,准备了运行测试的环境,用来模拟场景.

test_board_topics_view_success_status_code方法: 测试django是否对于现有的Board返回status code(状态码) 200(成功)

test_board_topics_view_not_found_status_code方法: 测试django是否对于不存在于数据库的Board返回status code 404(页面未找到)

test_board_topics_url_resolves_board_topics_view方法: 测试django是否使用了正确的视图函数去渲染topics.

运行测试: $ python3 mage.py test

访问: http://127.0.0.1:8081/boards/99/
DEBUG=False时,访问者会看到500 Error页面.

想要一个404 Page Not Found页面时,重写视图函数.
"""



class FirstTests(TestCase):
    def first_test(self):
        self.board = Board.objects.create(name='Django',description='Django board.')
        url = reverse('first')
        self.response = self.client.get(url)
    
    def test_first_view_status_code(self):
        self.assertEquals(self.response.status_code,200)

    def test_first_url_resolves_home_view(self):
        view = resolve('/')
        self.assertEquals(view.func,first)

    def test_first_view_contains_link_to_topics_page(self):
        board_topics_url = reverse('board_topics',kwargs={'pk': self.board.pk})
        self.assertContains(self.response,'href="{0}"'.format(board_topics_url))

"""
说明: 在FirstTests中添加了first_test方法,因为现在需要一个Board实例,将url和response移到了first_test,　这样可以在新测试中重用相同的response.
新测试是: test_first_view_contains_link_to_topics_page, 使用assertContains方法测试response主体部分是否包含给定的文本.
基本上是在测试response主体是否包含文本: href="/boards/1/".
"""
"""
class BoardTopicsTests(TestCase):
    def setUp(self):
        Board.objects.create(name='Django',description='Django board.')

    def test_board_topics_view_success_status_code(self):
        url = reverse('board_topics',kwargs={'pk': 1})
        response = self.client.get(url)
        # self.assertEquals(response.status_code,200)
        self.assertEquals(response.status_code,404)


    def test_board_topics_view_not_found_status_code(self):
        url = reverse('board_topics',kwargs={'pk':99})
        response = self.client.get(url)
        self.assertEquals(response.status_code,404)

    def test_board_topics_url_resolves_board_topics_view(self):
        view = resolve('/boards/1/')
        self.assertEquals(view.func,board_topics)
"""